package kafkaStudy2; 
//import util.properties packages
import java.util.Properties;

//import simple producer packages
import org.apache.kafka.clients.producer.Producer;

//import KafkaProducer packages
import org.apache.kafka.clients.producer.KafkaProducer;

//import ProducerRecord packages
import org.apache.kafka.clients.producer.ProducerRecord;

/** 
* @author 朱宏亮
* @version 创建时间：2017年12月13日 下午2:41:07 
* 类说明 直接使用 kafka_2.11-1.0.0，未整合spring
*/
public class ProducerStudy {

    public static void main(String[] args) {
        
        //Assign topicName to string variable
        String topicName = "zhlTopic";//args[0].toString();
        
        // create instance for properties to access producer configs   
        Properties props = new Properties();
        
        props.put("group.id", "GroupZHL01");
        
        //Assign localhost id
        props.put("bootstrap.servers", "localhost:9092");
        
        //Set acknowledgements for producer requests.      
        props.put("acks", "all");
        
        //If the request fails, the producer can automatically retry,
        props.put("retries", 0);
        
        //Specify buffer size in config
        props.put("batch.size", 16384);
        
        //Reduce the no of requests less than 0   
        props.put("linger.ms", 1);
        
        //The buffer.memory controls the total amount of memory available to the producer for buffering.   
        props.put("buffer.memory", 33554432);
        
        props.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer");
           
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        
        Producer<Integer, String> producer = new KafkaProducer<Integer, String>(props);
              
        for(int i = 0; i < 10; i++)
        	producer.send(new ProducerRecord<Integer, String>(topicName, Integer.valueOf(i), Integer.toString(i)));
    	System.out.println("Message sent successfully");
    	producer.close();
    }

}
 